package com.supkong.singleton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**购物车实现类
 * @author Kong
 * @date 2018/12/14
 * describe:
 */
public class Car {

    public static Car car = new Car();
    Map<Integer,List<Good>> goodCarMap = new HashMap<>();

    /**
     * 获取实例
     * @return
     */
    public static Car getInstance(){
        return car;
    }

    /**
     * 添加商品
     * @param id
     * @param good
     */
    public void add(int id,Good good){
        List<Good> goodList = goodCarMap.get(id);
        if(null==goodList){
            goodList = new ArrayList<>();
        }
        goodList.add(good);
        goodCarMap.put(id,goodList);
    }

    /**
     * 删除商品
     * @param id
     * @param good
     */
    public void del(int id , Good good){
        List<Good> goodList = goodCarMap.get(id);
        if(null==goodList){
            return;
        }
        if(goodList.contains(good)){
            goodList.remove(good);
        }
        goodCarMap.put(id,goodList);
    }
}
